package cn.zebra.dev.sms.huaxin.service.impl;

import cn.zebra.dev.sms.config.huaxin.SysSmsHuaXinConfigureBean;
import cn.zebra.dev.sms.huaxin.entity.MessageEntity;
import cn.zebra.dev.sms.huaxin.service.SysSmsService;
import cn.zebra.dev.utils.module.http.ssl.SSLClient;
import cn.zebra.dev.common.utils.StrUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author runnable@sina.cn
 */
@Service
public class SysSmsServiceImpl implements SysSmsService {

    @Autowired
    private SysSmsHuaXinConfigureBean configureBean;

    @Override
    public MessageEntity sendSingleMessage(String phone, String content) throws Exception {
        HttpClient httpclient = new SSLClient();
        HttpPost post = new HttpPost(configureBean.getGateway());
        post.setHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> pairList = new ArrayList<>();
        pairList.add(new BasicNameValuePair("action", "send"));
        pairList.add(new BasicNameValuePair("userid", ""));
        pairList.add(new BasicNameValuePair("account", configureBean.getUserName()));
        pairList.add(new BasicNameValuePair("password", configureBean.getPassword()));
        //多个手机号用逗号分隔
        pairList.add(new BasicNameValuePair("mobile", phone));
        pairList.add(new BasicNameValuePair("content", content));
        pairList.add(new BasicNameValuePair("sendTime", ""));
        pairList.add(new BasicNameValuePair("extno", ""));
        post.setEntity(new UrlEncodedFormEntity(pairList, "UTF-8"));

        HttpResponse response = httpclient.execute(post);
        HttpEntity entity = response.getEntity();
        String returnString = EntityUtils.toString(entity, "UTF-8");
        Document doc = DocumentHelper.parseText(returnString);
        Element rootElt = doc.getRootElement();
        MessageEntity record = new MessageEntity();
        record.setPhone(phone);
        record.setContent(content);
        String message = rootElt.elementText(MESSAGE).trim();
        record.setReturnMessage(message);
        String returnStatus = rootElt.elementText(RETURN_STATUS).trim();
        record.setReturnStatus(returnStatus);
        String task = rootElt.elementText(TASK_ID);
        record.setTaskId(StrUtils.isNotBlank(task) ? task.trim() : "");
        String remainPoint = rootElt.elementText(REMAIN_POINT);
        record.setRemainPoint(StrUtils.isNotBlank(remainPoint) ? Integer.parseInt(remainPoint.trim()) : 0);
        String success = rootElt.elementText(SUCCESS_COUNTS);
        record.setSuccessCount(StrUtils.isNotBlank(success) ? Integer.parseInt(success.trim()) : 0);
        if (returnStatus.contains(FAILD)) {
            return record;
        }
        EntityUtils.consume(entity);
        return record;
    }

}
